﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Endos.Wodice.Resources;

namespace Endos.Wodice
{
    public partial class MainPage : PhoneApplicationPage
    {
        private readonly DicePool dicePool;

        // Constructor
        public MainPage()
        {
            dicePool = new DicePool()
            {
                Difficulty = Difficulty.Eight,
                Explosion = Explosion.Ten
            };
            InitializeComponent();

            sliderDice.Value = 1;
            sliderDifficulty.Minimum = 4;
            sliderDifficulty.Value = 8;
            sliderExplosion.Minimum = 7;
            sliderExplosion.Value = 10;
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private void scollViewerGame_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            int? amountSuccess = null;
            var dices = dicePool.Roll(out amountSuccess);
            stackPanelGame.Children.Insert(0, new DiceControl(dices, dicePool.Quantity, amountSuccess));
        }

        private void sliderDice_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            dicePool.Quantity = Convert.ToInt32(Math.Truncate(sliderDice.Value));
            textBlockDice.Text = dicePool.Quantity.ToString(); 
        }

        private void sliderDifficulty_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            var difficulty = Convert.ToInt32(Math.Truncate(sliderDifficulty.Value));
            dicePool.Difficulty = (Difficulty)difficulty;
            textBlockDifficulty.Text = string.Format(AppResources.LabelDifficulty, difficulty); 
        }

        private void sliderExplosion_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            var explosion = Convert.ToInt32(Math.Truncate(sliderExplosion.Value));

            if (explosion <= 7)
            {
                explosion = 11;
                textBlockExplosion.Text = AppResources.WithoutExplosion;
            }
            else
            {
                textBlockExplosion.Text = string.Format(AppResources.LabelExplosion, explosion); 
            }

            dicePool.Explosion = (Explosion)explosion;
        }

        private void checkBoxCancellerSuccess_Checked(object sender, RoutedEventArgs e)
        {
            dicePool.CancellerSuccess = true;
        }

        private void checkBoxCancellerSuccess_Unchecked(object sender, RoutedEventArgs e)
        {
            dicePool.CancellerSuccess = false;
        }

        private void checkBoxOldSystem_Checked(object sender, RoutedEventArgs e)
        {
            dicePool.CancellerSuccess = true;
            dicePool.OldSystem = true;
            sliderDifficulty.IsEnabled = true;
            sliderDifficulty.Value = 6;
            stackPanelGame.Children.Clear();
        }

        private void checkBoxOldSystem_Unchecked(object sender, RoutedEventArgs e)
        {
            dicePool.CancellerSuccess = false;
            dicePool.OldSystem = false;
            sliderDifficulty.IsEnabled = false;
            sliderDifficulty.Value = 8;
            stackPanelGame.Children.Clear();
        }

        private void buttonInitiative_Click(object sender, RoutedEventArgs e)
        {
            stackPanelGame.Children.Insert(0, new DiceControl(dicePool.Quantity, dicePool.ThrowDice));
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}